#include "flow.h"
#include "eth.h"

#define FLOW_LINE_LEN 1000

enum {
    FLOW_TOK_SRC_MAC = 0,
    FLOW_TOK_DST_MAC,
    FLOW_TOK_SRC_IP, 
    FLOW_TOK_DST_IP,
    FLOW_TOK_MAX
};

static char line[FLOW_LINE_LEN];


static int
blank(char *str) 
{
    int i;
    int len = strnlen(str, FLOW_LINE_LEN);
    int blank = 1;

    for (i = 0; i < len; i++) {
        if (str[i] != ' '  && str[i] != '\n' && 
            str[i] != '\r' && str[i] != 0) { 
            blank = 0;
        }
    }

    return blank;
}


int 
timediff(struct timeval *b, struct timeval *e) 
{
    return (e->tv_sec - b->tv_sec) * 1000 + (e->tv_usec - b->tv_usec) / 1000;
}


int
parse_flows(char *fname, flow *flows, int nflows) 
{
    FILE *file;
    char *tok;
    int   ntok = 0;
    int   nline = 0;
    int   findex = 0;
    flow *flow;
    IP    addr;


    file = fopen(fname, "r+");
   
    if (file == NULL) {
        fprintf(stderr, "ERROR: error opening '%s'\n", fname);
        return -1;
    }

    memset(line, 0, FLOW_LINE_LEN);

    while (fgets(line, FLOW_LINE_LEN, file) != NULL) {

        nline++;
        ntok = 0;

        if (blank(line)) continue;

        flow = &flows[findex];
         
        tok = strtok(line, " ");

        while (tok != NULL) {
            switch (ntok) {
            case FLOW_TOK_SRC_MAC:
                

                break;

            case FLOW_TOK_DST_MAC:
                

                break;

            case FLOW_TOK_SRC_IP:
                if (tok[0] == '*' && strlen(tok) == 1) {
                    flow->sip = 0;
                } else {
                    addr = inet_addr(tok);
                    if (addr == INADDR_NONE) {
                        fprintf(stderr, "ERROR: (%s:%d) invalid src ip\n", fname, nline);
                        return -1;
                    } else {
                        flow->sip = addr;
                    }
                }                

                break;

            case FLOW_TOK_DST_IP:
                if (tok[0] == '*' && strlen(tok) == 1) {
                    fprintf(stderr, "ERROR: (%s:%d) dst ip cannot be *\n", fname, nline);
                    return -1;
                } else {
                    addr = inet_addr(tok);
                    if (addr == INADDR_NONE) {
                        fprintf(stderr, "ERROR: (%s:%d) invalid dst ip\n", fname, nline);
                        return -1;
                    } else {
                        flow->dip = addr;
                    }
                }

                break;

            default:
                if (tok[0] == '\n') ntok--;
                break;
            }
 
            ntok++;
            tok = strtok(NULL, " ");
        }

        findex++;

        memset(line, 0, FLOW_LINE_LEN);

        if (ntok != FLOW_TOK_MAX) {
            fprintf(stderr, "ERROR: (%s:%d) %d tokens, expected %d\n",
                    fname, nline, ntok, FLOW_TOK_MAX); 
            return -1;
        }
    }

    return findex;
}



    
    
